package com.huo.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.huo.constant.MessageConstant;
import com.huo.entity.PageResult;
import com.huo.entity.QueryPageBean;
import com.huo.entity.Result;
import com.huo.pojo.CheckGroup;
import com.huo.service.CheckGroupService;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import org.springframework.web.bind.annotation.*;

import javax.management.Query;
import java.util.List;

/**
 * @Description: 检查组控制层
 * @Author: 汪青松
 * @CreateDate: 2021/1/29 21:47
 */
@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;

    /**
     * @Description: 新增
     * @Author: 汪青松
     * @Param: [checkitemIds, checkGroup]
     * @Return: com.huo.entity.Result
     * @CreateDate: 2021/1/18 14:30
     */
    @PostMapping("/add")
    public Result add(Integer[] checkitemIds, @RequestBody CheckGroup checkGroup) {
        try {
            checkGroupService.add(checkGroup, checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    /**
     * @Description: 分页查询
     * @Author: 汪青松
     * @Param: [queryPageBean]
     * @Return: com.huo.entity.Result
     * @CreateDate: 2021/1/18 17:41
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = checkGroupService.findPage(queryPageBean);
        return pageResult;
    }

    /**
     * @Description: 根据id获取检查组信息
     * @Author: 汪青松
     * @Param: [checkgroupId]
     * @Return: com.huo.entity.Result
     * @CreateDate: 2021/1/19 14:55
     */
    @RequestMapping("/findById")
    public Result findById(Integer checkgroupId) {
        try {
            CheckGroup checkGroup = checkGroupService.findById(checkgroupId);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
     * @Description: 根据检查组id查询检查项ids
     * @Author: 汪青松
     * @Param: [checkgroupId]
     * @Return: com.huo.entity.Result
     * @CreateDate: 2021/1/19 15:01
     */
    @RequestMapping("/findCheckitemIdsById")
    public Result findCheckitemIdsById(Integer checkgroupId) {
        try {
            Integer[] checkitemIds = checkGroupService.findCheckitemIdsById(checkgroupId);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    /**
     * @Description: 编辑
     * @Author: 汪青松
     * @Param: [checkitemIds, checkGroup]
     * @Return: com.huo.entity.Result
     * @CreateDate: 2021/1/19 16:05
     */
    @RequestMapping("/edit")
    public Result edit(Integer[] checkitemIds, @RequestBody CheckGroup checkGroup) {
        try {
            checkGroupService.edit(checkGroup, checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    /**
     * @Description: 删除
     * @Author: 汪青松
     * @Param: [checkgroupId]
     * @Return: com.huo.entity.Result
     * @CreateDate: 2021/1/19 17:12
     */
    @RequestMapping("/delete")
    public Result delete(Integer checkgroupId) {
        try {
            checkGroupService.delete(checkgroupId);
        } catch (RuntimeException r) {
            return new Result(false, r.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    /**
     * @Description: 查询所有检查组
     * @Author: 汪青松
     * @Param: []
     * @Return: com.huo.entity.Result
     * @CreateDate: 2021/1/21 16:25-
     */
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<CheckGroup> checkGroupList = checkGroupService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroupList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
}

